package com.guitest.gui.jframe;


import com.guitest.gui.entity.User;
import com.guitest.gui.sqlutils.Insert;
import com.guitest.gui.userinfo.UserCode;
import com.guitest.gui.userinfo.UserTem;

import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

//主窗口
public class JFrameDemo extends JFrame {

    public void init(){
        this.setTitle("第10小组（student10）————罗世举、王雨石、刘恒");
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //放东西 容器
        Container container = this.getContentPane();

        //绝对布局
        //container.setLayout(null);

        JButton button = new JButton("欢迎测温");
        button.setBounds(150,45,200,50);
        button.setBackground(new Color(84, 247, 90));
        //button.setHorizontalAlignment(SwingConstants.CENTER);
        //布局
        JPanel panel01 = new JPanel(new GridLayout(1,1));
        panel01.setBounds(0,0,500,500);

        //设置图片
        JLabel label = new JLabel();
        URL url = JFrameDemo.class.getResource("索隆.png");
        //System.out.println(url);
        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setBounds(0,0,500,500);

        //点击按钮，弹出测温者信息弹框
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                //弹窗
                new MyDialogDemo();
            }
        });

        panel01.setBackground(new Color(102, 107, 222));
        container.setBackground(new Color(255, 179, 141, 138));
        label.add(button);
        panel01.add(label);
        container.add(panel01);
        //窗口设置为不能改变大小
        this.setResizable(false);
    }
}

//测温者输入信息弹窗
/*class MyDialogDemo extends JDialog {
    private String username;
    private String userCode;
    private double tem;

    public MyDialogDemo(){
        this.setResizable(false);
        this.setTitle("测温者信息输入");
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        //容器
        Container container = this.getContentPane();
        //container.setLayout(null);

        //创建元素
        JLabel label01 = new JLabel("测温者姓名:");
        JLabel label02 = new JLabel("测温者信息码:");
        LimitNumTextField textField01 = new LimitNumTextField(10);//测温者姓名输入框
        JButton button01 = new JButton(">点击生成信息码<");
        JButton button02 = new JButton("开始测温");

        //布局
        JPanel panel01 = new JPanel(new GridLayout(1,2));
        JPanel panel02 = new JPanel(new GridLayout(1,2));
        JPanel panel03 = new JPanel(new GridLayout(3,1,10,10));
        label01.setBounds(30,30,80,50);
        label02.setBounds(30,60,80,50);
        label01.setHorizontalAlignment(SwingConstants.CENTER);//label居中显示
        label02.setHorizontalAlignment(SwingConstants.CENTER);
        textField01.setBounds(30,30,100,50);
        button01.setBounds(30,30,200,50);

        panel01.setBounds(30,30,200,100);
        panel02.setBounds(30,60,200,100);

        //事件
        //MyActionListener01 myActionListener01 = new MyActionListener01();//输入框回车事件
        //textField01.addActionListener(myActionListener01);
        textField01.addFocusListener(new MyActionListener03());//输入框失去焦点事件
        //点击按钮，生成信息码
        button01.addActionListener(new MyActionListener02());//测温者信息码按钮事件
        //开始测温按钮事件
        button02.addActionListener(new MyActionListener04());

        //先将所有元素放在不同的面板（JPanel）中
        button01.setBackground(new Color(255, 252, 172));
        button02.setBackground(new Color(255, 156, 1));
        panel01.setBackground(new Color(189, 245, 255));
        panel02.setBackground(new Color(189, 245, 255));
        panel03.setBackground(new Color(6, 237, 255));
        panel01.add(label01);
        panel01.add(textField01);
        panel02.add(label02);
        panel02.add(button01);

        panel03.add(panel01);
        panel03.add(panel02);
        panel03.add(button02);

        //最后再将JPanel放到容器Container中
        container.add(panel03);
    }

    //内部类，01 输入框事件（失去焦点）
    private class MyActionListener03 implements FocusListener{
        @Override
        public void focusGained(FocusEvent focusEvent) {

        }

        @Override
        public void focusLost(FocusEvent focusEvent) {
            JTextField field = (JTextField) focusEvent.getSource();
            username = field.getText();
            if (username.equals("")){
                System.out.println("姓名不能为空");
                //姓名为空时的警告弹窗
                new MyDialogDemo02();
            }
            else{
                System.out.println("测温者姓名:" + username);
            }
        }
    }

    //内部类，02 测温者信息码按钮事件
    class MyActionListener02 implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            if (username.equals("")){
                System.out.println("姓名不能为空");
                //姓名为空时的警告弹窗
                new MyDialogDemo02();
            }
            else{
                UserCode oneUserCode = new UserCode();
                userCode = oneUserCode.getRandomNickname();
                System.out.println("10位信息码:" + userCode);
            }
        }
    }

    //内部类，03 姓名为空时的警告弹窗
    class MyDialogDemo02 extends JDialog {
        public MyDialogDemo02(){
            this.setResizable(false);
            this.setTitle("警告");
            this.setVisible(true);
            this.setBounds(380,150,100,80);

            Container container = this.getContentPane();
            //container.setLayout(null);

            //创建元素
            JLabel label01 = new JLabel("姓名不能为空");

            //布局
            JPanel panel01 = new JPanel(new GridLayout(1,1));
            label01.setBounds(30,30,80,50);
            panel01.setBounds(30,30,200,100);

            panel01.setBackground(Color.red);
            panel01.add(label01);
            container.add(panel01);
        }
    }

    //内部类，04 开始测温按钮事件
    class MyActionListener04 implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            if(username.equals("")){
                System.out.println("姓名不能为空");
                //姓名为空时的警告弹窗
                new MyDialogDemo02();
            }else if(userCode == null){
                System.out.println("请写了名字后，点生成击信息码按钮");
                //信息码为空时的警告弹窗
                new MyDialogDemo03();
            }else {
                tem = new UserTem().Number();
                //new Insert().InsertTest(username,userCode,tem);
                //new Insert().InsertTest("mk","123456",40);
                User user = new User(username, userCode, tem);
                System.out.println(user);
                //展示测温者信息的弹窗
                new MyDialogDemo04();
            }
        }
    }

    //内部类，05 信息码为空时的警告弹窗
    class MyDialogDemo03 extends JDialog {
        public MyDialogDemo03(){
            this.setResizable(false);
            this.setTitle("警告");
            this.setVisible(true);
            this.setBounds(380,370,180,70);

            Container container = this.getContentPane();
            //container.setLayout(null);

            //创建元素
            JLabel label01 = new JLabel("请写了名字后，点击本按钮");

            //布局
            JPanel panel01 = new JPanel(new GridLayout(1,1));
            label01.setBounds(30,30,80,50);
            panel01.setBounds(30,30,200,100);

            panel01.setBackground(Color.red);
            panel01.add(label01);
            container.add(panel01);
        }
    }

    //内部类，06 展示测温者信息的弹窗
    class MyDialogDemo04 extends JDialog {
        public MyDialogDemo04(){
            this.setResizable(false);
            this.setTitle("当前测温者信息");
            this.setVisible(true);
            this.setBounds(230,470,250,120);

            Container container = this.getContentPane();
            //container.setLayout(null);

            //创建元素
            JLabel label01 = new JLabel("当前测温者姓名：" + username);
            JLabel label02 = new JLabel("当前测温者信息码：" + userCode);
            JLabel label03 = new JLabel("当前测温者体温：" + tem);
            //布局
            JPanel panel01 = new JPanel(new GridLayout(3,1));
            label01.setBounds(30,30,80,50);
            label02.setBounds(30,60,80,50);
            label03.setBounds(30,90,80,50);
            panel01.setBounds(30,30,200,100);

            panel01.setBackground(new Color(188, 242, 74));
            panel01.add(label01);
            panel01.add(label02);
            panel01.add(label03);
            container.add(panel01);
        }
    }
}*/

//重做文本域（字数限制）
class LimitNumTextField extends JTextField {
    public LimitNumTextField(int length) {
        setDocument(new LimitNumDocument(length));
    }

    private class LimitNumDocument extends PlainDocument {
        private int fLength = -1; // 可任意输入

        public LimitNumDocument(int length) {
            fLength = length;
        }

        public void insertString(int offs, String str, AttributeSet attr)
                throws BadLocationException {
            int originalLength = getLength();
            if (originalLength <= 0) {
                super.insertString(offs, str, attr);
                return;
            }

            char[] input = str.toCharArray();
            int inputLength = 0;
            for (int i = 0; i < input.length; i++) {
                if (originalLength + inputLength >= fLength) {
                    break;
                }
                inputLength++;
            }

            super.insertString(offs, new String(input, 0, inputLength), attr);
        }
    }
}

//输入框事件（回车）
/*class MyActionListener01 implements ActionListener{
    private String username;

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        JTextField field = (JTextField) actionEvent.getSource();
        username = field.getText();
        if (username.equals("")){
            System.out.println("姓名不能为空");
        }
        else{
            System.out.println("测温者姓名:" + username);
        }
    }

}*/

//输入框事件（失去焦点）
/*class MyActionListener03 implements FocusListener{
    private String username;
    @Override
    public void focusGained(FocusEvent focusEvent) {

    }

    @Override
    public void focusLost(FocusEvent focusEvent) {
        JTextField field = (JTextField) focusEvent.getSource();
        username = field.getText();
        if (username.equals("")){
            System.out.println("姓名不能为空");
            new MyDialogDemo02();
        }
        else{
            System.out.println("测温者姓名:" + username);
        }
    }
}*/

//测温者信息码按钮事件
/*class MyActionListener02 implements ActionListener{
    private String userCode;
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        UserCode oneUserCode = new UserCode();
        userCode = oneUserCode.getRandomNickname();
        System.out.println("10位信息码:" + userCode);
    }
}*/

//姓名为空时的警告弹窗
/*class MyDialogDemo02 extends JDialog {
    public MyDialogDemo02(){
        this.setVisible(true);
        this.setBounds(380,150,100,80);

        Container container = this.getContentPane();
        //container.setLayout(null);

        //创建元素
        JLabel label01 = new JLabel("姓名不能为空");

        //布局
        JPanel panel01 = new JPanel(new GridLayout(1,1));
        label01.setBounds(30,30,80,50);
        panel01.setBounds(30,30,200,100);

        panel01.setBackground(Color.red);
        panel01.add(label01);
        container.add(panel01);
    }
}*/


